#ifndef TWO_STAGE_MOVEMENT_POSITION_H
#define TWO_STAGE_MOVEMENT_POSITION_H

/** Class to maintain and calculate the current movement and position */
class MyMovementPosition
{
protected:
	/** Start time of movement */
	int m_iStartTime;
	/** Mid time of movement */
	int m_iMidTime;
	/** End time of movement */
	int m_iEndTime;
	/** Start X coordinate of movement */
	int m_iStartX;
	/** Start Y coordinate of movement */
	int m_iStartY;
	/** Mid X coordinate of movement */
	int m_iMidX;
	/** Mid Y coordinate of movement */
	int m_iMidY;
	/** End X coordinate of movement */
	int m_iEndX;
	/** End Y coordinate of movement */
	int m_iEndY;

	/** Calculated current X coordinate for current time */
	int m_iCurrentX;
	/** Calculated current Y coordinate for current time */
	int m_iCurrentY;


public:
	/** Constructor */
	MyMovementPosition()
		: m_iStartTime(0), m_iMidTime(0), m_iEndTime(0)
		, m_iStartX(0), m_iStartY(0)
		, m_iMidX(0), m_iMidY(0)
		, m_iEndX(0), m_iEndY(0)
		, m_iCurrentX(0), m_iCurrentY(0)
	{
	}

	/** Set up movement - start and finish coordinates and time */
	inline void Setup( int iXStart, int iYStart, int iXMid, int iYMid, int iXEnd, int iYEnd, int iStartTime, int iEndTime )
	{
		m_iStartX = iXStart;
		m_iStartY = iYStart;
		m_iMidX = iXMid;
		m_iMidY = iYMid;
		m_iEndX = iXEnd;
		m_iEndY = iYEnd;
		m_iStartTime = iStartTime;
		m_iEndTime = iEndTime;
		m_iMidTime = (iStartTime + iEndTime)/2;
		// Initially put position at start
		m_iCurrentX = m_iStartX;
		m_iCurrentY = m_iStartY;
	}

	/** Calculate current position, for the current time */
	inline void Calculate( int iCurrentTime )
	{
		// At start position until start time
		if ( iCurrentTime <= m_iStartTime )
		{
			m_iCurrentX = m_iStartX;
			m_iCurrentY = m_iStartY;
			return;
		}
		// At end position after end time
		if ( iCurrentTime >= m_iEndTime )
		{
			m_iCurrentX = m_iEndX;
			m_iCurrentY = m_iEndY;
			return;
		}

		// If more than half-way through then go to the second set
		if ( iCurrentTime >= ((m_iStartTime+m_iEndTime)/2) )
		{
			// Calculate the current position while mid-movement
			m_iCurrentX = m_iMidX 
				+ ((iCurrentTime-m_iMidTime) * (m_iEndX-m_iMidX))/(m_iEndTime-m_iMidTime);
			m_iCurrentY = m_iMidY 
				+ ((iCurrentTime-m_iMidTime) * (m_iEndY-m_iMidY))/(m_iEndTime-m_iMidTime);
		}
		else
		{ // In first half
			// Calculate the current position while mid-movement
			m_iCurrentX = m_iStartX 
				+ ((iCurrentTime-m_iStartTime) * (m_iMidX-m_iStartX))/(m_iMidTime-m_iStartTime);
			m_iCurrentY = m_iStartY 
				+ ((iCurrentTime-m_iStartTime) * (m_iMidY-m_iStartY))/(m_iMidTime-m_iStartTime);
		}
	}

	/** Get calculated X coordinate */
	inline int GetX() { return m_iCurrentX; }

	/** Get calculated Y coordinate */
	inline int GetY() { return m_iCurrentY; }

	/** Check whether movement has ended */
	inline bool HasMovementFinished( int iCurrentTime ) { return iCurrentTime >= m_iEndTime; }
};

#endif
